package com.coocaa.ops.plus.dashboard.v1.codegen.convert;

import com.coocaa.ops.plus.dashboard.v1.codegen.constant.TypeConstants;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.PackageConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.TableConfigInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : bijiahao
 * @date : 2019/4/25.
 * @description: 包路径转化工具
 */
public class PackagePathConvert {

    public static Map<String, String> convertPackagePath(List<TableConfigInfo> tableConfigInfoList, PackageConfigModel packageConfig) {
        Map<String, String> pathInfo = new HashMap<>(9);

        for (TableConfigInfo tableConfigInfo : tableConfigInfoList) {
            if (TypeConstants.ENTITY.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.ENTITY, convert2EntityPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.MAPPER.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.MAPPER, convert2MapperPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.XML.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.XML, convert2XmlPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.MODEL.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.MODEL, convert2ModelPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.BIZ.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.BIZ, convert2BizPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.BIZ_IMPL.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.BIZ_IMPL, convert2BizImplPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.PROVIDER.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.PROVIDER, convert2ProviderPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.PROVIDER_IMPL.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.PROVIDER_IMPL, convert2ProviderImplPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.SERVICE.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.SERVICE, convert2ServicePackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.SERVICE_IMPL.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.SERVICE_IMPL, convert2ServiceImplPackage(tableConfigInfo, packageConfig));

            } else if (TypeConstants.CONTROLLER.equals(tableConfigInfo.getType())) {
                pathInfo.put(TypeConstants.CONTROLLER, convert2ControllerPackage(tableConfigInfo, packageConfig));
            } else {
                throw new RuntimeException("没有对应类型处理，type：" + tableConfigInfo.getType());
            }
        }
        return pathInfo;
    }

    /**
     * 实体类-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2EntityPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getEntity());
    }


    /**
     * Mapper接口-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2MapperPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getMapper());

    }

    /**
     * Xml文件-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2XmlPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getXml());

    }

    /**
     * 查询Model类-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ModelPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getModel());

    }

    /**
     * Biz接口-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2BizPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getBiz());

    }

    /**
     * Biz实现-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2BizImplPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getBizImpl());

    }

    /**
     * Service接口-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ProviderPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getProvider());

    }

    /**
     * Service实现-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ProviderImplPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getProviderImpl());

    }

    /**
     * Service接口-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ServicePackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getService());

    }

    /**
     * Service实现-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ServiceImplPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getServiceImpl());

    }

    /**
     * 控制器-包路径
     *
     * @param tableConfig
     * @param packageConfig
     * @return
     */
    public static String convert2ControllerPackage(TableConfigInfo tableConfig, PackageConfigModel packageConfig) {
        return covertPackageForSubPackageFront(tableConfig, packageConfig.getController());
    }

    /**
     * 转化包路径-子包名放在包配置路径的前面
     *
     * @param tableConfig
     * @param packageConfigPath
     * @return
     */
    private static String covertPackageForSubPackageFront(TableConfigInfo tableConfig, String packageConfigPath) {
        StringBuilder packagePath = new StringBuilder();
        packagePath.append(tableConfig.getPackageRootName());
        if (StringUtils.isNotBlank(tableConfig.getSubPackageName())) {
            packagePath.append(".")
                    .append(tableConfig.getSubPackageName());
        }
        if (StringUtils.isNotBlank(packageConfigPath)) {
            packagePath.append(".")
                    .append(packageConfigPath);
        }
        return packagePath.toString();
    }

}
